{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import struct\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import time, datetime\n",
    "import multiprocessing\n",
    "import talib as tdx\n",
    "from easyquant import MongoIo\n",
    "import matplotlib.pyplot as plt\n",
    "from easyquant.indicator.base import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def readTdxLdayFile(fname=\"sh000001.day\"):\n",
    "  dataSet=[]\n",
    "  with open(fname,'rb') as fl:\n",
    "    buffer=fl.read() #读取数据到缓存\n",
    "    size=len(buffer) \n",
    "    rowSize=32 #通信达day数据，每32个字节一组数据\n",
    "    code=os.path.basename(fname).replace('.day','')\n",
    "    for i in range(0,size,rowSize): #步长为32遍历buffer\n",
    "      row=list( struct.unpack('IIIIIfII',buffer[i:i+rowSize]) )\n",
    "      row[1]=row[1]/100\n",
    "      row[2]=row[2]/100\n",
    "      row[3]=row[3]/100\n",
    "      row[4]=row[4]/100\n",
    "      row.pop() #移除最后无意义字段\n",
    "      row.insert(0,code)\n",
    "      dataSet.append(row) \n",
    "  data=pd.DataFrame(data=dataSet,columns=['code','tradeDate','open','high','low','close','amount','vol'])\n",
    "  data['date'] = data['tradeDate'].apply(lambda x: datetime.datetime.strptime(str(x),'%Y%m%d').date())\n",
    "  data=data.set_index(['date']) #.sort_index()\n",
    "#   data=data.set_index(['date'])\n",
    "#   data=data.set_index(['date']) #.sort_index()\n",
    "  return code, data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def recalc(code, data):\n",
    "    if data.size == 0:\n",
    "        return\n",
    "    beg_date = data.index[0]\n",
    "    end_date = data.index[-1]\n",
    "    today = datetime.datetime.today().date()\n",
    "    ds = None\n",
    "    while beg_date <= today:\n",
    "        if beg_date in data.index:\n",
    "            ds = data.loc[beg_date]\n",
    "        else:\n",
    "            ds.name = beg_date\n",
    "            data = data.append(ds)\n",
    "        beg_date = beg_date + datetime.timedelta(1)\n",
    "    data = data.sort_index()\n",
    "#     data.to_csv('%s.csv'%code)\n",
    "    return data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getStockByDate(stockData, date,code):\n",
    "    if date < stockData.index[0][0]:\n",
    "        return None\n",
    "    try:\n",
    "        return stockData.loc[date,code]\n",
    "    except:\n",
    "        return getStockByDate(stockData, date - datetime.timedelta(1), code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getValidDate(year, month, day, nextday = True):\n",
    "    try:\n",
    "        return pd.Timestamp(year, month, day)\n",
    "    except:\n",
    "        if nextday:\n",
    "            return getValidDate(year, month + 1, 1)\n",
    "        else:\n",
    "            return getValidDate(year, month, day - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2010-12-01 00:00:00')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getValidDate(2010,11,31)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def beforeDate(calcDate, year = 0, month = 0):\n",
    "    year = year + int(month / 12)\n",
    "    month = month - int(month / 12) * 12\n",
    "    if calcDate.month > month:\n",
    "        result = getValidDate(calcDate.year - year, calcDate.month - month, calcDate.day)\n",
    "    else:\n",
    "        result = getValidDate(calcDate.year - year - 1, calcDate.month + 12 - month, calcDate.day)\n",
    "    \n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def afterDate(calcDate, year = 0, month = 0):\n",
    "    year = year + int(month / 12)\n",
    "    month = month - int(month / 12) * 12\n",
    "    if calcDate.month + month > 12:\n",
    "        result = getValidDate(calcDate.year + year + 1, calcDate.month + month - 12, calcDate.day)\n",
    "    else:\n",
    "        result = getValidDate(calcDate.year + year, calcDate.month + month, calcDate.day)\n",
    "    \n",
    "    return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calcBAYMDateLst(calcDate, dstDate, year, month, before = True):\n",
    "    out = []\n",
    "    if before:\n",
    "        value = eval('beforeDate')(calcDate, year, month)\n",
    "        out.append(value)\n",
    "        while value > dstDate:\n",
    "            calcDate = value\n",
    "            value = eval('beforeDate')(calcDate, year, month)\n",
    "            out.append(value)\n",
    "    else:\n",
    "        value = eval('afterDate')(calcDate, year, month)\n",
    "        out.append(value)\n",
    "        while value < dstDate:\n",
    "            calcDate = value\n",
    "            value = eval('afterDate')(calcDate, year, month)\n",
    "            out.append(value)\n",
    "    return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calcBAYMDate(calcDate, year, month, before = True):\n",
    "    if before:\n",
    "        value = eval('beforeDate')(calcDate, year, month)\n",
    "    else:\n",
    "        value = eval('afterDate')(calcDate, year, month)\n",
    "    return value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "today = datetime.date.today()\n",
    "dstDate = datetime.date(2021,4,2)\n",
    "o1=calcBAYMDate(dstDate, 7, 6, False)\n",
    "o1\n",
    "# testDate(today, 'afterDate', 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def GannCheckOld(dataIn, posB0Day, gannBase = {'y':30,'m':0}, gannList = [{'y':7,'m':6},{'y':2,'m':6},{'y':0,'m':7},{'y':0,'m':7}]):\n",
    "#     data=dataIn.reset_index()\n",
    "#     data=data.set_index(['date'])\n",
    "#     lstDay = data.index[-1]\n",
    "    code = data.index[-1][1] ##二维索引用\n",
    "    lastDay = data.index[-1][0] ##数据最大日期\n",
    "    x0data = getStockByDate(data, posB0Day, code) #基础点1的数据\n",
    "    xedata = getStockByDate(data, lastDay, code) #基础点1的数据 \n",
    "    x1wight = 1 ##权重\n",
    "    pxedata = 0\n",
    "    pdelta = 0\n",
    "    tj0=gannList[0] ##基础计算目标\n",
    "    ##基础点2\n",
    "    posB1Day = calcBAYMDate(posB0Day, gannBase['y'], gannBase['m'], False)\n",
    "    pos0Day = None\n",
    "    pos1Day = None\n",
    "    if posB1Day > lastDay:\n",
    "        tj1=gannList[0]\n",
    "        base2 = calcBAYMDate(posB1Day, tj1['y'], tj1['m'], True)\n",
    "        base1 = calcBAYMDate(base2, tj1['y'], tj1['m'], True)\n",
    "        if base2 < lastDay and base1 < lastDay:\n",
    "            pos0Day = base1\n",
    "            pos1Day = base2\n",
    "            x0data = getStockByDate(data, pos0Day, code)\n",
    "            x1data=getStockByDate(data, pos1Day, code)\n",
    "            x1wight = x1data.close / x0data.close\n",
    "            \n",
    "            pxedata = getStockByDate(data, calcBAYMDate(lastDay, tj1['y'], tj1['m'], True), code) * x1wight\n",
    "            pdelta = abs((pxedata.close - xedata.close ) / xedata.close )\n",
    "            if pdelta > 0.05:\n",
    "                x1wight = x1data.close / x0data.close\n",
    "            else:\n",
    "                x1wight = x1data.close / x0data.close\n",
    "    else:\n",
    "        x1data = getStockByDate(data, out1, code)\n",
    "        x1wight = x1data.close / x0data.close\n",
    "    \n",
    "    return {'posB0':posB0Day, 'posB1':posB1Day, 'pos0':pos0Day, 'pos1':pos1Day, 'w':x1wight, 'pdata':pxedata, 'pd':pdelta}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def GannCheck(dataIn, posB0Day, gannBase = {'y':30,'m':0}, gannList = [{'y':7,'m':6},{'y':2,'m':6},{'y':0,'m':7},{'y':0,'m':1}]):\n",
    "#     data=dataIn.reset_index()\n",
    "#     data=data.set_index(['date'])\n",
    "#     lstDay = data.index[-1]\n",
    "    code = data.index[-1][1] ##二维索引用\n",
    "    lastDay = data.index[-1][0] ##数据最大日期\n",
    "    x0data = getStockByDate(data, posB0Day, code) #基础点1的数据\n",
    "    xedata = getStockByDate(data, lastDay, code) #基础点1的数据 \n",
    "    x1wight = 1 ##权重\n",
    "    pxedata = 0\n",
    "    pdelta = 0\n",
    "    tj0=gannList[0] ##基础计算目标\n",
    "    ##基础点2\n",
    "    posB1Day = calcBAYMDate(posB0Day, gannBase['y'], gannBase['m'], False)\n",
    "    pos0Day = None\n",
    "    pos1Day = None\n",
    "    if posB1Day > lastDay:\n",
    "        result = GannSubCheck(data, posB1Day, gannList)\n",
    "        if result != None:\n",
    "            result['posB0'] = posB0Day\n",
    "            result['posB1'] = posB1Day\n",
    "        return result\n",
    "    else:\n",
    "        print('mloop3', base2)\n",
    "        pos0Day = posB0Day\n",
    "        pos1Day = posB1Day\n",
    "        x0data = getStockByDate(data, pos0Day, code)\n",
    "        x1data=getStockByDate(data, pos1Day, code)\n",
    "        x1wight = x1data.close / x0data.close\n",
    "\n",
    "        pxedata = getStockByDate(data, calcBAYMDate(lastDay, gannBase['y'], gannBase['m'], True), code) * x1wight\n",
    "        pdelta = abs((pxedata.close - xedata.close ) / xedata.close )\n",
    "    \n",
    "        return {'posB0':posB0Day, 'posB1':posB1Day, 'pos0':pos0Day, 'pos1':pos1Day, 'w':x1wight, 'pdata':pxedata, 'pd':pdelta}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def GannSubCheck(data, posB1Day, gannList, delta = 0.1):\n",
    "    code = data.index[-1][1] ##二维索引用\n",
    "    lastDay = data.index[-1][0] ##数据最大日期\n",
    "    xedata = getStockByDate(data, lastDay, code) #基础点1的数据\n",
    "\n",
    "    tj1=gannList[0]\n",
    "    pos0Day = None\n",
    "    pos1Day = None\n",
    "    x1wight = 1 ##权重\n",
    "    pxedata = 0\n",
    "    pdelta = 0\n",
    "    base2 = calcBAYMDate(posB1Day, tj1['y'], tj1['m'], True)\n",
    "    base1 = calcBAYMDate(base2, tj1['y'], tj1['m'], True)\n",
    "    if base2 < lastDay and base1 < lastDay:\n",
    "        pos0Day = base1\n",
    "        pos1Day = base2\n",
    "        x0data = getStockByDate(data, pos0Day, code)\n",
    "        x1data=getStockByDate(data, pos1Day, code)\n",
    "        x1wight = x1data.close / x0data.close\n",
    "\n",
    "        pxedata = getStockByDate(data, calcBAYMDate(lastDay, tj1['y'], tj1['m'], True), code) * x1wight\n",
    "        pdelta = abs((pxedata.close - xedata.close ) / xedata.close )\n",
    "        if pdelta > delta:\n",
    "            if len(gannList) <= 1:\n",
    "#                 print('None')\n",
    "                return None\n",
    "            else:\n",
    "#                 print('loop1', gannList[1:], posB1Day, pdelta)\n",
    "                return GannSubCheck(data, posB1Day, gannList[1:], delta)\n",
    "        else:\n",
    "#             print('loop2', gannList, posB1Day, pdelta)\n",
    "            return {'pos0':pos0Day, 'pos1':pos1Day, 'w':x1wight, 'tj1':tj1}\n",
    "    else:\n",
    "#         print('loop3', base2)\n",
    "        return GannSubCheck(data, base2, gannList, delta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th>code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-03-31</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.75</td>\n",
       "      <td>17.96</td>\n",
       "      <td>17.62</td>\n",
       "      <td>17.78</td>\n",
       "      <td>111591.0</td>\n",
       "      <td>198215968.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-01</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.74</td>\n",
       "      <td>18.46</td>\n",
       "      <td>17.73</td>\n",
       "      <td>18.38</td>\n",
       "      <td>193919.0</td>\n",
       "      <td>353269632.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-02</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.22</td>\n",
       "      <td>18.48</td>\n",
       "      <td>18.10</td>\n",
       "      <td>18.28</td>\n",
       "      <td>151472.0</td>\n",
       "      <td>276234592.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-06</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.20</td>\n",
       "      <td>18.58</td>\n",
       "      <td>17.72</td>\n",
       "      <td>17.89</td>\n",
       "      <td>180264.0</td>\n",
       "      <td>324674080.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-07</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.80</td>\n",
       "      <td>18.49</td>\n",
       "      <td>17.71</td>\n",
       "      <td>18.45</td>\n",
       "      <td>203201.0</td>\n",
       "      <td>368431136.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    open   high    low  close    volume       amount\n",
       "date       code                                                     \n",
       "2021-03-31 600827  17.75  17.96  17.62  17.78  111591.0  198215968.0\n",
       "2021-04-01 600827  17.74  18.46  17.73  18.38  193919.0  353269632.0\n",
       "2021-04-02 600827  18.22  18.48  18.10  18.28  151472.0  276234592.0\n",
       "2021-04-06 600827  18.20  18.58  17.72  17.89  180264.0  324674080.0\n",
       "2021-04-07 600827  17.80  18.49  17.71  18.45  203201.0  368431136.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m=MongoIo()\n",
    "# data=m.get_index_day('000001', st_end = '2022-09-01', st_start = '1990-05-28')\n",
    "data=m.get_stock_day('600827', st_end = '2022-09-01', st_start = '1990-05-28')\n",
    "data.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'pos0': Timestamp('2021-03-06 00:00:00'),\n",
       " 'pos1': Timestamp('2021-04-06 00:00:00'),\n",
       " 'w': 1.0975460122699385,\n",
       " 'tj1': {'y': 0, 'm': 1},\n",
       " 'posB0': Timestamp('1997-05-06 00:00:00'),\n",
       " 'posB1': Timestamp('2027-05-06 00:00:00')}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outs=GannCheck(data, pd.Timestamp(1997,5,6), gannBase = {'y':30,'m':0}, gannList = [{'y':7,'m':6},{'y':2,'m':6},{'y':0,'m':7},{'y':0,'m':1}])\n",
    "outs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>ycz</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th>code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2021-03-15</th>\n",
       "      <th>600827</th>\n",
       "      <td>16.49</td>\n",
       "      <td>18.04</td>\n",
       "      <td>16.47</td>\n",
       "      <td>18.04</td>\n",
       "      <td>307595.0</td>\n",
       "      <td>545724864.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-16</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.30</td>\n",
       "      <td>18.55</td>\n",
       "      <td>17.75</td>\n",
       "      <td>18.35</td>\n",
       "      <td>425092.0</td>\n",
       "      <td>779496832.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-17</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.10</td>\n",
       "      <td>18.66</td>\n",
       "      <td>18.03</td>\n",
       "      <td>18.60</td>\n",
       "      <td>237596.0</td>\n",
       "      <td>437052064.0</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-18</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.40</td>\n",
       "      <td>19.10</td>\n",
       "      <td>18.36</td>\n",
       "      <td>18.57</td>\n",
       "      <td>290061.0</td>\n",
       "      <td>543996480.0</td>\n",
       "      <td>20.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-19</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.29</td>\n",
       "      <td>18.82</td>\n",
       "      <td>18.14</td>\n",
       "      <td>18.63</td>\n",
       "      <td>248983.0</td>\n",
       "      <td>461459392.0</td>\n",
       "      <td>20.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-22</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.78</td>\n",
       "      <td>18.88</td>\n",
       "      <td>17.70</td>\n",
       "      <td>18.11</td>\n",
       "      <td>322062.0</td>\n",
       "      <td>587211072.0</td>\n",
       "      <td>20.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-23</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.25</td>\n",
       "      <td>18.29</td>\n",
       "      <td>16.93</td>\n",
       "      <td>17.27</td>\n",
       "      <td>303629.0</td>\n",
       "      <td>533068704.0</td>\n",
       "      <td>19.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-24</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.50</td>\n",
       "      <td>17.62</td>\n",
       "      <td>16.54</td>\n",
       "      <td>16.57</td>\n",
       "      <td>253426.0</td>\n",
       "      <td>430812800.0</td>\n",
       "      <td>19.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-25</th>\n",
       "      <th>600827</th>\n",
       "      <td>16.66</td>\n",
       "      <td>18.18</td>\n",
       "      <td>16.29</td>\n",
       "      <td>17.25</td>\n",
       "      <td>315514.0</td>\n",
       "      <td>546835008.0</td>\n",
       "      <td>18.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-26</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.17</td>\n",
       "      <td>17.91</td>\n",
       "      <td>17.11</td>\n",
       "      <td>17.61</td>\n",
       "      <td>194640.0</td>\n",
       "      <td>340772832.0</td>\n",
       "      <td>17.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-29</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.58</td>\n",
       "      <td>17.69</td>\n",
       "      <td>17.04</td>\n",
       "      <td>17.37</td>\n",
       "      <td>147173.0</td>\n",
       "      <td>254698480.0</td>\n",
       "      <td>17.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-30</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.28</td>\n",
       "      <td>18.26</td>\n",
       "      <td>17.13</td>\n",
       "      <td>17.92</td>\n",
       "      <td>202153.0</td>\n",
       "      <td>360847168.0</td>\n",
       "      <td>17.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-03-31</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.75</td>\n",
       "      <td>17.96</td>\n",
       "      <td>17.62</td>\n",
       "      <td>17.78</td>\n",
       "      <td>111591.0</td>\n",
       "      <td>198215968.0</td>\n",
       "      <td>17.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-01</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.74</td>\n",
       "      <td>18.46</td>\n",
       "      <td>17.73</td>\n",
       "      <td>18.38</td>\n",
       "      <td>193919.0</td>\n",
       "      <td>353269632.0</td>\n",
       "      <td>17.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-02</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.22</td>\n",
       "      <td>18.48</td>\n",
       "      <td>18.10</td>\n",
       "      <td>18.28</td>\n",
       "      <td>151472.0</td>\n",
       "      <td>276234592.0</td>\n",
       "      <td>17.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-06</th>\n",
       "      <th>600827</th>\n",
       "      <td>18.20</td>\n",
       "      <td>18.58</td>\n",
       "      <td>17.72</td>\n",
       "      <td>17.89</td>\n",
       "      <td>180264.0</td>\n",
       "      <td>324674080.0</td>\n",
       "      <td>17.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-07</th>\n",
       "      <th>600827</th>\n",
       "      <td>17.80</td>\n",
       "      <td>18.49</td>\n",
       "      <td>17.71</td>\n",
       "      <td>18.45</td>\n",
       "      <td>203201.0</td>\n",
       "      <td>368431136.0</td>\n",
       "      <td>17.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-08</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.187571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-09</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16.902209</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-12</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.999755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-13</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.999755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-14</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.999755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-15</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>19.799730</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-16</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.139969</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-19</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.447282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-20</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.447282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-21</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>20.447282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-22</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>19.876558</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-23</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.954620</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-04-26</th>\n",
       "      <th>600827</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.45</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>19.327785</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    open   high    low  close    volume       amount  \\\n",
       "date       code                                                        \n",
       "2021-03-15 600827  16.49  18.04  16.47  18.04  307595.0  545724864.0   \n",
       "2021-03-16 600827  18.30  18.55  17.75  18.35  425092.0  779496832.0   \n",
       "2021-03-17 600827  18.10  18.66  18.03  18.60  237596.0  437052064.0   \n",
       "2021-03-18 600827  18.40  19.10  18.36  18.57  290061.0  543996480.0   \n",
       "2021-03-19 600827  18.29  18.82  18.14  18.63  248983.0  461459392.0   \n",
       "2021-03-22 600827  18.78  18.88  17.70  18.11  322062.0  587211072.0   \n",
       "2021-03-23 600827  18.25  18.29  16.93  17.27  303629.0  533068704.0   \n",
       "2021-03-24 600827  17.50  17.62  16.54  16.57  253426.0  430812800.0   \n",
       "2021-03-25 600827  16.66  18.18  16.29  17.25  315514.0  546835008.0   \n",
       "2021-03-26 600827  17.17  17.91  17.11  17.61  194640.0  340772832.0   \n",
       "2021-03-29 600827  17.58  17.69  17.04  17.37  147173.0  254698480.0   \n",
       "2021-03-30 600827  17.28  18.26  17.13  17.92  202153.0  360847168.0   \n",
       "2021-03-31 600827  17.75  17.96  17.62  17.78  111591.0  198215968.0   \n",
       "2021-04-01 600827  17.74  18.46  17.73  18.38  193919.0  353269632.0   \n",
       "2021-04-02 600827  18.22  18.48  18.10  18.28  151472.0  276234592.0   \n",
       "2021-04-06 600827  18.20  18.58  17.72  17.89  180264.0  324674080.0   \n",
       "2021-04-07 600827  17.80  18.49  17.71  18.45  203201.0  368431136.0   \n",
       "2021-04-08 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-09 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-12 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-13 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-14 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-15 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-16 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-19 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-20 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-21 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-22 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-23 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "2021-04-26 600827    NaN    NaN    NaN  18.45       NaN          NaN   \n",
       "\n",
       "                         ycz  \n",
       "date       code               \n",
       "2021-03-15 600827   0.000000  \n",
       "2021-03-16 600827   0.000000  \n",
       "2021-03-17 600827   0.000000  \n",
       "2021-03-18 600827  20.000000  \n",
       "2021-03-19 600827  20.000000  \n",
       "2021-03-22 600827  20.000000  \n",
       "2021-03-23 600827  19.000000  \n",
       "2021-03-24 600827  19.000000  \n",
       "2021-03-25 600827  18.000000  \n",
       "2021-03-26 600827  17.000000  \n",
       "2021-03-29 600827  17.000000  \n",
       "2021-03-30 600827  17.000000  \n",
       "2021-03-31 600827  17.000000  \n",
       "2021-04-01 600827  17.000000  \n",
       "2021-04-02 600827  17.000000  \n",
       "2021-04-06 600827  17.000000  \n",
       "2021-04-07 600827  17.000000  \n",
       "2021-04-08 600827  17.187571  \n",
       "2021-04-09 600827  16.902209  \n",
       "2021-04-12 600827  17.999755  \n",
       "2021-04-13 600827  17.999755  \n",
       "2021-04-14 600827  17.999755  \n",
       "2021-04-15 600827  19.799730  \n",
       "2021-04-16 600827  20.139969  \n",
       "2021-04-19 600827  20.447282  \n",
       "2021-04-20 600827  20.447282  \n",
       "2021-04-21 600827  20.447282  \n",
       "2021-04-22 600827  19.876558  \n",
       "2021-04-23 600827  18.954620  \n",
       "2021-04-26 600827  19.327785  "
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# m=MongoIo()\n",
    "# # data=m.get_index_day('000001', st_end = '2022-09-01', st_start = '1990-05-28')\n",
    "# data=m.get_stock_day('600827', st_end = '2022-09-01', st_start = '1990-05-28')\n",
    "# outs=GannCheck(data, pd.Timestamp(1992,5,26), gannBase = {'y':30,'m':0}, gannList = [{'y':7,'m':6},{'y':2,'m':6},{'y':0,'m':7},{'y':0,'m':1}])\n",
    "# outs=GannCheck(data, pd.Timestamp(1997,5,6), gannBase = {'y':30,'m':0}, gannList = [{'y':7,'m':6},{'y':2,'m':6},{'y':0,'m':7},{'y':0,'m':1}])\n",
    "# outs\n",
    "data['ycz'] = 0\n",
    "x1wight=outs['w']\n",
    "# print(x1wight)\n",
    "gannBase=outs['tj1']\n",
    "# print(gannBase)\n",
    "lastDay = data.index[-1][0]\n",
    "lastDay\n",
    "code='600827'\n",
    "for rday in range(-20, 20):\n",
    "    ycday = lastDay + datetime.timedelta(rday)\n",
    "#     print(ycday)\n",
    "    preN = getStockByDate(data, calcBAYMDate(ycday, gannBase['y'], gannBase['m'], True), code) * x1wight\n",
    "    preD = afterDate(preN.name[0], year=0, month = 7)\n",
    "    try:\n",
    "        rclose = data.at[(ycday,code),'close']\n",
    "#         print(ycday)\n",
    "        rclose = getStockByDate(data, ycday, code).close\n",
    "        diff = preN.close - rclose\n",
    "    except Exception as e:\n",
    "#         print(preN.name[0])\n",
    "        rclose = 0.0\n",
    "        diff = 0\n",
    "#     print(str(preD)[:10], int(preN.close), rclose, int(diff))\n",
    "    if rclose == 0 and ycday < lastDay:\n",
    "#         print(ycday)\n",
    "        continue\n",
    "    else:\n",
    "        if ycday.weekday() > 4:\n",
    "            continue\n",
    "        rclose = getStockByDate(data, ycday, code).close\n",
    "#     print(ycday.weekday())\n",
    "    data.at[(ycday,code),'ycz']  = preN.close\n",
    "    data.at[(ycday,code),'close']  = rclose\n",
    "data.tail(30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "xlabel = []\n",
    "for x in data.iloc[-24:].index:\n",
    "    xlabel.append(str(x[0])[5:10])\n",
    "data.iloc[-24:].close.plot(x=xlabel,figsize=(400,500))\n",
    "data.iloc[-24:].ycz.plot(x=xlabel,figsize=(400,500))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.at[(data.index[-1][0],'000001'),'ycz']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data.at[[data.index[-1][0],code],'ycz'] = 123\n",
    "data.at[data.index[-1],'ycz']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.index[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(data.iloc[-60])\n",
    "np.array(data.iloc[-60:].index.levels[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data3=data.iloc[:-50]\n",
    "# print(data3.index[-1])\n",
    "outs4 = []\n",
    "for x in data.iloc[-30:].index:\n",
    "    outs4.append(data.loc[x].close)\n",
    "\n",
    "#     print(data.loc[x].close)\n",
    "# outs = GannSubCheck(data3, pd.Timestamp(2022,10,16), gannList = [{'y':7,'m':6},{'y':2,'m':6},{'y':0,'m':7},{'y':0,'m':1}])\n",
    "# outs['prelist']\n",
    "# outs1 = GannSubCheck(data3, pd.Timestamp(2022,4,26), gannList = [{'y':7,'m':6},{'y':2,'m':6},{'y':0,'m':7},{'y':0,'m':1}])\n",
    "# outs1['prelist']\n",
    "print(len(out4))\n",
    "x = range(-10,20)\n",
    "print(x)\n",
    "# plt.plot(x, outs)\n",
    "plt.title(\"Matplotlib demo\") \n",
    "plt.xlabel(\"x axis caption\") \n",
    "plt.ylabel(\"y axis caption\") \n",
    "plt.plot(x,outs['prelist']) \n",
    "plt.plot(x,outs1['prelist']) \n",
    "plt.plot(x,outs4) \n",
    "# plt.plot(np.array(data.iloc[-60:].index.levels[0]),np.array(data.iloc[-60:].close))\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.iloc[-60:].close.plot()\n",
    "data.iloc[-60:].high.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "getStockByDate(data, pd.Timestamp(2013,10,2),'000001')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calcBADDate(calcDate, dstDate, day, before = True):\n",
    "    out = []\n",
    "    value = None\n",
    "    if before:\n",
    "        while value == None or value > dstDate:\n",
    "            value = calcDate - datetime.timedelta(day)\n",
    "            out.append(value)\n",
    "            calcDate = value\n",
    "    else:\n",
    "        while value == None or value < dstDate:\n",
    "            value = calcDate + datetime.timedelta(day)\n",
    "            out.append(value)\n",
    "            calcDate = value\n",
    "    return out    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "calcBADDate(dstDate, today, 50, False)\n",
    "calcBADDate(today, dstDate, 20, True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "calcGannDate(dstDate, today, 30, False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calcGannDate(calcDate, dstDate, base = 360, before = True):\n",
    "    mdays = abs((calcDate - dstDate).days)\n",
    "    \n",
    "    intValue = True\n",
    "    if not isinstance (base, int):\n",
    "        intValue = False\n",
    "    yValue = []\n",
    "    yLabel = 'y' + str(base)\n",
    "    sValue = []\n",
    "    sLabel = 's' + str(base)\n",
    "    mValue = []\n",
    "    mLabel = 'm' + str(base)\n",
    "    wValue = []\n",
    "    wLabel = 'w' + str(base)\n",
    "    dValue = []\n",
    "    dLabel = 'd' + str(base)\n",
    "    if mdays > base * 365:\n",
    "        ## 年\n",
    "        if isinstance (base, int):\n",
    "            year = base\n",
    "            month = 0\n",
    "        else:\n",
    "            year = int(base)\n",
    "            month = int((base * 10 - int(base) * 10) * 12 / 10)\n",
    "        yValueT = calcBAYMDate(calcDate, dstDate, year, month, before)\n",
    "    if intValue and mdays > base * 90:\n",
    "        # 季\n",
    "        year = int(base / 3)\n",
    "        month = (base - 3 * int(base/3)) * 3\n",
    "        sValue = calcBAYMDate(calcDate, dstDate, year, month, before)\n",
    "    if intValue and mdays > base * 30:\n",
    "        # 月\n",
    "        year = int(base / 12)\n",
    "        month = base - 12 * int(base / 12) \n",
    "        mValue = calcBAYMDate(calcDate, dstDate, year, month, before)\n",
    "    if intValue and mdays > base * 7:\n",
    "        # 周\n",
    "        wValue = calcBADDate(calcDate, dstDate, base * 7, before)\n",
    "    if intValue and mdays > base:\n",
    "        # 日\n",
    "        dValue = calcBADDate(calcDate, dstDate, base, before)\n",
    "    return {yLabel: yValue, sLabel: sValue, mLabel: mValue, wLabel: wValue, dLabel: dValue}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datetime.datetime.strptime('2012-12-12','%Y-%m-%d').date()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gannCheck(dates, before = True, bday = 20, aday = 10):\n",
    "    today = datetime.date.today()\n",
    "    odate = []\n",
    "    for sdate in dates:\n",
    "        cdate = datetime.datetime.strptime(sdate,'%Y%m%d').date()\n",
    "#         print(cdate, today)\n",
    "        if before:\n",
    "            outs = gannDateAll(today, cdate, before)\n",
    "        else:\n",
    "            outs = gannDateAll(cdate, today, before)\n",
    "        \n",
    "#         return outs\n",
    "        for out in outs:\n",
    "#             print(out.keys)\n",
    "            for d in out:\n",
    "                gdate = out[d]\n",
    "#                 print(gdate)\n",
    "                if gdate == None:\n",
    "                    continue\n",
    "                else:\n",
    "                    lout = []\n",
    "                    for ldate in gdate:\n",
    "                        cdays = abs((ldate - today).days)\n",
    "                        if ldate > today:\n",
    "                            if cdays < aday:\n",
    "                                lout.append(ldate)\n",
    "                        else:\n",
    "                            if cdays < bday:\n",
    "                                lout.append(ldate)\n",
    "#                         if abs((ldate - today).days) < 30:\n",
    "#                             lout.append(ldate)\n",
    "                    if len(lout) > 0:\n",
    "                        odate.append({d:lout})\n",
    "#             return out\n",
    "    return odate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gannDateAll(calcDate, fstDate, before = True):\n",
    "    result = []\n",
    "    result.append(calcGannDate(calcDate, fstDate, 360, before))\n",
    "    result.append(calcGannDate(calcDate, fstDate, 180, before))\n",
    "    result.append(calcGannDate(calcDate, fstDate, 120, before))\n",
    "    result.append(calcGannDate(calcDate, fstDate, 90, before))\n",
    "    result.append(calcGannDate(calcDate, fstDate, 60, before))\n",
    "    result.append(calcGannDate(calcDate, fstDate, 30, before))\n",
    "    result.append(calcGannDate(calcDate, fstDate, 28, before))\n",
    "    result.append( calcGannDate(calcDate, fstDate, 15, before))\n",
    "    result.append(calcGannDate(calcDate, fstDate, 10, before))\n",
    "    result.append(calcGannDate(calcDate, fstDate, 7, before))\n",
    "    result.append(calcGannDate(calcDate, fstDate, 2.5, before))\n",
    "#     return (y360, y180, y120, y90, y60, y30, y28, y15, y10, y7, y2d5)\n",
    "#     return result\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "datetime.date(2020,10,31)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data.head()?\n",
    "data.loc[data.high.idxmax()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1993-02-15\n",
    "a = gannCheck(['19930601'], False, aday = 30, bday = 10)\n",
    "a = gannCheck(['19930216'], False, aday = 30, bday = 10)\n",
    "# a = gannCheck(['19940729'], False, aday = 30, bday = 10)\n",
    "# 1994-07-29\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for b in a:\n",
    "    print(a[b])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "beforeDate(datetime.date(2021,4,2), 7, 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def jeDateCalc(calcDate, fstDate, base = 360):\n",
    "    mdays = (calcDate - fstDate).days\n",
    "    intValue = True\n",
    "    if not isinstance (base, int):\n",
    "        intValue = False\n",
    "    yValue = None\n",
    "    yLabel = 'y' + str(base)\n",
    "    sValue = None\n",
    "    sLabel = 's' + str(base)\n",
    "    mValue = None\n",
    "    mLabel = 'm' + str(base)\n",
    "    wValue = None\n",
    "    wLabel = 'w' + str(base)\n",
    "    dValue = None\n",
    "    dLabel = 'd' + str(base)\n",
    "    if mdays > base * 365:\n",
    "        ## 年\n",
    "        if isinstance (base, int):\n",
    "            year = base\n",
    "            month = 0\n",
    "        else:\n",
    "            year = int(base)\n",
    "            month = int((base * 10 - int(base) * 10) * 12 / 10)\n",
    "        yValue = beforeDate(calcDate, year, month)\n",
    "    if intValue and mdays > base * 90:\n",
    "        # 季\n",
    "        year = int(base / 3)\n",
    "        month = (base - 3 * int(base/3)) * 3\n",
    "        sValue = beforeDate(calcDate, year, month)\n",
    "    if intValue and mdays > base * 30:\n",
    "        # 月\n",
    "        year = int(base / 12)\n",
    "        month = base - 12 * int(base / 12) \n",
    "        mValue = beforeDate(calcDate, year, month)\n",
    "    if intValue and mdays > base * 7:\n",
    "        # 周\n",
    "        wValue = calcDate - datetime.timedelta(base * 7)\n",
    "    if intValue and mdays > base:\n",
    "        # 日\n",
    "        dValue = calcDate - datetime.timedelta(base)\n",
    "    return {yLabel: yValue, sLabel: sValue, mLabel: mValue, wLabel: wValue, dLabel: dValue}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def jeDateAll(calcDate, fstDate):\n",
    "    y360 = jeDateCalc(calcDate, fstDate, 360)\n",
    "    y180 = jeDateCalc(calcDate, fstDate, 180)\n",
    "    y120 = jeDateCalc(calcDate, fstDate, 120)\n",
    "    y90 = jeDateCalc(calcDate, fstDate, 90)\n",
    "    y60 = jeDateCalc(calcDate, fstDate, 60)\n",
    "    y30 = jeDateCalc(calcDate, fstDate, 30)\n",
    "    y28 = jeDateCalc(calcDate, fstDate, 28)\n",
    "    y15 = jeDateCalc(calcDate, fstDate, 15)\n",
    "    y10 = jeDateCalc(calcDate, fstDate, 10)\n",
    "    y7 = jeDateCalc(calcDate, fstDate, 7)\n",
    "    y2d5 = jeDateCalc(calcDate, fstDate, 2.5)\n",
    "    return (y360, y180, y120, y90, y60, y30, y28, y15, y10, y7, y2d5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(code, data) = readTdxLdayFile()\n",
    "data2=recalc(code, data)\n",
    "data3 = data2.copy()\n",
    "data3[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "jedate=jeDateAll(cdata, bdata)\n",
    "jedate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cdata = datetime.date(2021,2,18)\n",
    "bdata = datetime.date(1990,2,18) \n",
    "df = cdata  - bdata\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "jedate[0]['d360']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "calcDate = jedate[0]['d360']\n",
    "calcDate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calcJePos(calcDate, befDay = 3, aftDay = 3):\n",
    "    bnum = befDay\n",
    "    enum = aftDay\n",
    "\n",
    "    beg=0\n",
    "    end=0\n",
    "    mhigh=0\n",
    "    mlow=0\n",
    "    hday=0\n",
    "    lday=0\n",
    "    graph=''\n",
    "    while beg == mhigh or beg == mlow or end == mhigh or end == mlow:\n",
    "        bnum += 1\n",
    "        enum += 1\n",
    "        bd=calcDate - datetime.timedelta(bnum)\n",
    "        ed=calcDate + datetime.timedelta(enum)\n",
    "\n",
    "        df3=data3.loc[bd:ed]\n",
    "        beg=df3.index[0]\n",
    "        end=df3.index[-1]\n",
    "        mhigh=df3.iloc[df3.close.argmax()].name\n",
    "        mlow=df3.iloc[df3.close.argmin()].name\n",
    "        hday = (calcDate - mhigh).days\n",
    "        lday = (calcDate - mlow).days\n",
    "        \n",
    "        graph = calcGraph(calcDate, mhigh, mlow)\n",
    "        \n",
    "    return {'high':mhigh, 'low':mlow, 'befDay':bnum, 'aftDay':enum, 'hday':hday, 'lday':lday, 'graph':graph}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calcGraph(calcDate, mhigh, mlow):        \n",
    "    graph = ''\n",
    "    if mhigh < mlow:## H-L\n",
    "        if mhigh < calcDate and mlow > calcDate:\n",
    "            graph = 'HCL'\n",
    "        else:\n",
    "            if calcDate > mlow:\n",
    "                grapho = 'HLC'\n",
    "            else:\n",
    "                graph = 'CHL'\n",
    "    else: ## L-H\n",
    "        if mlow < calcDate and mhigh > calcDate:\n",
    "            graph = 'LCH'\n",
    "        else:\n",
    "            if calcDate > mhigh:\n",
    "                grapho = 'LHC'\n",
    "            else:\n",
    "                graph = 'CLH'\n",
    "    return graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calcBefDay(calcDate, befDay = 10):\n",
    "    df3=data3[calcDate - datetime.timedelta(befDay):calcDate]\n",
    "    mhigh=df3.iloc[df3.close.argmax()].name\n",
    "    mlow=df3.iloc[df3.close.argmin()].name\n",
    "    hday = (calcDate - mhigh).days\n",
    "    lday = (calcDate - mlow).days\n",
    "    graph = calcGraph(calcDate, mhigh, mlow)\n",
    "    print(mhigh, mlow, calcDate)\n",
    "    if hday == befDay or lday == befDay:\n",
    "        befDay += 1\n",
    "        return calcBefDay(calcDate, befDay)\n",
    "    else:\n",
    "        return {'hday':hday, 'lday':lday, 'day':befDay, 'graph':graph}\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# print(calcJePos(calcDate, befDay = 10, aftDay = 10))\n",
    "print(calcDate)\n",
    "print(calcJePos(calcDate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(calcBefDay(calcDate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bnum = 0\n",
    "enum = 0\n",
    "\n",
    "beg=0\n",
    "end=0\n",
    "mhigh=0\n",
    "mlow=0\n",
    "\n",
    "while beg == mhigh or beg == mlow or end == mhigh or end == mlow:\n",
    "    bnum += 1\n",
    "    enum += 1\n",
    "    bd=calcDate - datetime.timedelta(bnum)\n",
    "    ed=calcDate + datetime.timedelta(enum)\n",
    "\n",
    "    df3=data3.loc[bd:ed]\n",
    "    beg=df3.index[0]\n",
    "    end=df3.index[-1]\n",
    "    mhigh=df3.iloc[df3.close.argmax()].name\n",
    "    mlow=df3.iloc[df3.close.argmin()].name\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(data3.iloc[data3.close.argmax()].name)\n",
    "print(data3.iloc[data3.close.argmin()].name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calcGannPos(data, year = 0, month = 0, days = 0):\n",
    "    if step == 0:\n",
    "        hdate = data.iloc[data3.close.argmax()].name\n",
    "        hvalue = data.loc[hdate]\n",
    "        ldate = data.iloc[data3.close.argmin()].name\n",
    "        lvalue = data.loc[ldate]\n",
    "        return {'h': hvalue, 'l': lvalue}\n",
    "    else:\n",
    "#         if type = 'y':\n",
    "        beg = data.index[0]\n",
    "        ndate = afterDate(beg, step, 0)\n",
    "        end = data.index[-1]\n",
    "        outa=[]\n",
    "        while ndate < end:\n",
    "            cdata = data.loc[beg:ndate]\n",
    "            hdate = cdata.iloc[cdata.close.argmax()].name\n",
    "            ldate = cdata.iloc[cdata.close.argmin()].name\n",
    "            t1 = {'h': data.loc[hdate], 'l': data.loc[ldate]}\n",
    "            outa.append(t1)\n",
    "            beg = ndate + datetime.timedelta(1)\n",
    "            ndate = afterDate(beg, step, 0)\n",
    "            \n",
    "        return outa\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "calcGannPos(data3, step = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('h',mhigh)\n",
    "print('l', mlow)\n",
    "print('b',beg)\n",
    "print('e',end)\n",
    "print(calcDate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data3.loc[calc_date]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "calc_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "calc_date = datetime.date.today()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=data.loc[beg_date]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.name=datetime.date(1990, 12, 20)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfn = pd.DataFrame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfn=dfn.append(df)\n",
    "\n",
    "dfn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(dfn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "today = datetime.datetime.today().date()\n",
    "data.loc[beg_date]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df1=data.loc[beg_date]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "end_date = datetime.date(1990,12,31)\n",
    "end_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d2=data[0:9].copy()\n",
    "d2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# d2.at[datetime.date(1990,12,22),'code']='sh111'\n",
    "ndf = d2.loc[datetime.date(1990,12,21)]\n",
    "# d2.append(datetime.date(1990,12,22))\n",
    "ndf['name']='1234'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ndf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ndf.index['name']=datetime.date(1990,12,22)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data=data.set_index(['tradeDate'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df=datetime.datetime.strptime(str(data.index[0]),'%Y%m%d').date()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from easyquant import MongoIo\n",
    "from easyquant.indicator.base import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m=MongoIo()\n",
    "data=m.get_index_day('000001', st_end = '2022-09-01', st_start = '1993-05-28')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.loc[data.close.idxmin()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "VOL = data.volume\n",
    "CLOSE = data.close\n",
    "C = data.close\n",
    "H = data.high\n",
    "L = data.low\n",
    "O = data.open\n",
    "VAR2 = CLOSE * VOL\n",
    "VAR3 = EMA((EMA(VAR2, 3) / EMA(VOL, 3) + EMA(VAR2, 6) / EMA(VOL, 6)\n",
    "            + EMA(VAR2, 12) / EMA(VOL, 12) + EMA(VAR2, 24) / EMA(VOL, 24)) / 4, 13)\n",
    "白线 = 1.06 * VAR3\n",
    "MA4 = MA(C, 4)\n",
    "MA24 = MA(C, 24)\n",
    "# C1 = C >= MA4\n",
    "# C2 = C < MA4\n",
    "#\n",
    "# # IF(MA4 >= REF(MA4, 1), MA4, DRAWNULL), COLORRED, LINETHICK2;\n",
    "JJ = (3 * C + H + L + O) / 6\n",
    "VAR1 = (8 * JJ + 7 * REF(JJ, 1) + 6 * REF(JJ, 2) + 5 * REF(JJ, 3) + 4 * REF(JJ, 4)\n",
    "        + 3 * REF(JJ, 5) + 2 * REF(JJ, 6) + REF(JJ, 8)) / 36\n",
    "TJ2 = IFAND4(VOL == HHV(VOL, 10), VOL > 2 * REF(VOL, 1), CLOSE > VAR1, C > REF(C, 1), True, False)\n",
    "# LJL = FILTER(TJ2, 5)\n",
    "TJ1 = (JJ-白线) /白线 * 100\n",
    "\n",
    "HVOL = HHV(VOL, 34)\n",
    "HNUM = BARSLAST(HVOL == VOL)\n",
    "HH = REF(H, HNUM)\n",
    "HL = REF(L, HNUM)\n",
    "JZBD = IFAND4(HNUM > 10, C > HL, H > HH, C < HH * 1.06, True, False)\n",
    "JZBD_S = IFAND3(HNUM > 10, C > HL, H > HH, True, False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.close.argmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "HNUM[90:]\n",
    "# HH[185:]\n",
    "# C[185:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "BARSLAST(HH == VOL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def tdx_bjmm_jzmd(data):\n",
    "    # AMOUNT = data.amount\n",
    "    VOL = data.volume\n",
    "    CLOSE = data.close\n",
    "    C = data.close\n",
    "    H = data.high\n",
    "    L = data.low\n",
    "    O = data.open\n",
    "    VAR2 = CLOSE * VOL\n",
    "    VAR3 = EMA((EMA(VAR2, 3) / EMA(VOL, 3) + EMA(VAR2, 6) / EMA(VOL, 6)\n",
    "                + EMA(VAR2, 12) / EMA(VOL, 12) + EMA(VAR2, 24) / EMA(VOL, 24)) / 4, 13)\n",
    "    白线 = 1.06 * VAR3\n",
    "    MA4 = MA(C, 4)\n",
    "    MA24 = MA(C, 24)\n",
    "    # C1 = C >= MA4\n",
    "    # C2 = C < MA4\n",
    "    #\n",
    "    # # IF(MA4 >= REF(MA4, 1), MA4, DRAWNULL), COLORRED, LINETHICK2;\n",
    "    JJ = (3 * C + H + L + O) / 6\n",
    "    VAR1 = (8 * JJ + 7 * REF(JJ, 1) + 6 * REF(JJ, 2) + 5 * REF(JJ, 3) + 4 * REF(JJ, 4)\n",
    "            + 3 * REF(JJ, 5) + 2 * REF(JJ, 6) + REF(JJ, 8)) / 36\n",
    "    TJ2 = IFAND4(VOL == HHV(VOL, 10), VOL > 2 * REF(VOL, 1), CLOSE > VAR1, C > REF(C, 1), True, False)\n",
    "    # LJL = FILTER(TJ2, 5)\n",
    "    TJ1 = (JJ-白线) /白线 * 100\n",
    "\n",
    "    HVOL = HHV(VOL, 34)\n",
    "    HNUM = BARSLAST(HVOL == VOL)\n",
    "    HH = REF(H, HNUM)\n",
    "    HL = REF(L, HNUM)\n",
    "    JZBD = IFAND4(HNUM > 10, C > HL, H > HH, C < HH * 1.06, True, False)\n",
    "    JZBD_S = IFAND3(HNUM > 10, C > HL, H > HH, True, False)\n",
    "    # # MACDTJ\n",
    "    # SHORT =12\n",
    "    # LONG = 26\n",
    "    # MID = 9\n",
    "    # EMASHORT = EMA(CLOSE, SHORT)\n",
    "    # EMALONG = EMA(CLOSE, LONG)\n",
    "    # DIF = EMASHORT - EMALONG\n",
    "    # DEA = EMA(DIF, MID)\n",
    "    # MACD = (DIF-DEA) * 2\n",
    "    # # MACDTJ = IFAND4(MACD>0, DIF > 0, DEA > 0, CROSS(DIF, DEA), True, False)\n",
    "    # MACDTJ = IFAND3(MACD > 0, DIF > 0, DEA > 0, True, False)\n",
    "\n",
    "    # B_1 = IFAND3(TJ1 >= 0, REF(TJ1,1) < 0, MACDTJ, 1, 0)\n",
    "    # B_1 = IFAND(TJ1 >= 0, REF(TJ1, 1) < 0, 1, 0)\n",
    "    # B_1 = IFAND4(TJ1 > 0, REF(TJ1, 1) < 0, TJ2, MACDTJ, 1, 0)\n",
    "    # B_1 = IFAND3(TJ1 > 0, REF(TJ1, 1) < 0, TJ2, 1, 0)\n",
    "    B_1 = IFAND4(TJ1 > 0, REF(TJ1, 1) < 0, TJ2, JZBD, 1, 0)\n",
    "    S_1 = IFAND(TJ1 < 0, REF(TJ1, 1) > 0, 1, -1)\n",
    "    # S_1 = IFAND(C < MA24, REF(C,1) > REF(MA24,1), 1, -1)\n",
    "    # return B_1, B_1, False\n",
    "    # return B_1, -1, False\n",
    "    return B_1, S_1, False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "H3=tdx_bjmm_jzmd(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a,b,c = H3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "b.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QUANTAXIS as QA\n",
    "import pyecharts.options as opts\n",
    "from pyecharts.charts import Candlestick\n",
    "from pyecharts.globals import CurrentConfig, NotebookType\n",
    "CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB\n",
    "\n",
    "code=[\"000735\"]\n",
    "start_date=\"2020-01-01\"\n",
    "end_date=\"2021-01-01\"\n",
    "\n",
    "ax = QA.QA_fetch_stock_day_adv(code,start_date, end_date).data.reset_index(1)\n",
    "# data = QA.QA_fetch_stock_day_adv(['000001', '000002', '000004', '600000'], '2017-09-01', '2018-05-20')\n",
    "\n",
    "x_data = ax.index.map(str).values.tolist()\n",
    "y_data = ax.loc[:,['open',  'close','low','high']].values.tolist()\n",
    "\n",
    "r =(\n",
    "    Candlestick(init_opts=opts.InitOpts(width=\"1440px\", height=\"720px\"))\n",
    "    .add_xaxis(xaxis_data=x_data)\n",
    "    .add_yaxis(series_name=\"\", y_axis=y_data)\n",
    "    .set_series_opts()\n",
    "    .set_global_opts(\n",
    "        xaxis_opts=opts.AxisOpts(is_scale=True),\n",
    "        title_opts=opts.TitleOpts(title=code),\n",
    "    )\n",
    ")\n",
    "r.load_javascript()\n",
    "r.render_notebook()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stamp=pd.Timestamp(2021,4,8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str(stamp)[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "time.strptime(stamp, '%Y')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "qawork",
   "language": "python",
   "name": "qawork"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
